package git.soulbgm.controller;

import git.soulbgm.pojo.FileInfo;
import git.soulbgm.utils.MinioUtil;
import org.apache.commons.compress.utils.IOUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * MinIO控制器
 *
 * @author SoulBGM
 * @date 2023-05-22
 */
@RestController
public class MinioController {

    private final MinioUtil minioUtil;
    private final static String SUCCESS = "success";
    public MinioController(MinioUtil minioUtil) {
        this.minioUtil = minioUtil;
    }

    @GetMapping("createBucket/{bucket}")
    public String createBucket(@PathVariable("bucket") String bucket) throws Exception {
        minioUtil.createBucket(bucket);
        return SUCCESS;
    }

    @PostMapping("uploadFile/{bucket}")
    public String uploadFile(MultipartFile file, @PathVariable("bucket") String bucket) throws Exception {
        InputStream inputStream = file.getInputStream();
        minioUtil.uploadFile(inputStream, bucket, file.getOriginalFilename());
        inputStream.close();
        return SUCCESS;
    }

    @GetMapping("listBuckets")
    public List<String> listBuckets() throws Exception {
        return minioUtil.listBuckets();
    }

    @GetMapping("listFiles/{bucket}")
    public List<FileInfo> listFiles(@PathVariable("bucket") String bucket) {
        return minioUtil.listFiles(bucket);
    }

    @GetMapping("download/{bucket}/{objectName}")
    public void download(@PathVariable("bucket") String bucket, @PathVariable("objectName") String objectName, HttpServletResponse response) throws Exception {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        String fileName = bucket + "_" + objectName;
        response.setHeader("Content-Disposition", String.format("attachment; filename=%s", new String(fileName.getBytes("gb2312"), "ISO8859-1")));
        InputStream inputStream = minioUtil.download(bucket, objectName);
        byte[] bytes = IOUtils.toByteArray(inputStream);
        ServletOutputStream outputStream = response.getOutputStream();
        outputStream.write(bytes);
        inputStream.close();
        outputStream.flush();
        outputStream.close();
    }

    @GetMapping("deleteBucket/{bucket}")
    public String deleteBucket(@PathVariable("bucket") String bucket) throws Exception {
        minioUtil.deleteBucket(bucket);
        return SUCCESS;
    }

    @GetMapping("deleteObject/{bucket}/{objectName}")
    public String deleteObject(@PathVariable("bucket") String bucket, @PathVariable("objectName") String objectName) throws Exception {
        minioUtil.deleteObject(bucket, objectName);
        return SUCCESS;
    }

}
